package com.mrzhou.log.publisher;

import com.mrzhou.core.tool.utils.*;
import com.mrzhou.log.annotation.ApiLog;
import com.mrzhou.log.constant.LogConstant;
import com.mrzhou.log.entity.LogApi;
import com.mrzhou.log.event.ApiLogEvent;
import com.mrzhou.log.support.GeneralHandlerLog;
import com.mrzhou.web.util.WebUtil;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

public class ApiLogPublisher {

    public static void publisherEvent(String className, String methodName, long time, Date requestTime, Object parameters, Object result) {
        LogApi log = new LogApi();
        log.setMethodClass(className);
        log.setMethodName(methodName);
        log.setResultObjects(JacksonUtil.toJsonString(result));
        log.setParameters(JacksonUtil.toJsonString(parameters));
        log.setTime(time);
        log.setRequestTime(LocalDateUtil.toLocalDateTime(requestTime));
        // 添加从request获取的基本信息
        GeneralHandlerLog.addRequestToLog(log);

        Map<String, Object> map = new HashMap<>();
        map.put(LogConstant.EVENT_LOG, log);
        SpringUtil.publishEvent(new ApiLogEvent(map));
    }

}
